package com.sevenGroup.pawnshop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sevenGroup.pawnshop.Utils.CopyProperties;
import com.sevenGroup.pawnshop.Utils.Result;
import com.sevenGroup.pawnshop.controller.form.GoodsForm;
import com.sevenGroup.pawnshop.controller.form.ShopSelectConditionalForm;
import com.sevenGroup.pawnshop.controller.info.GoodsInfo;
import com.sevenGroup.pawnshop.model.Goods;
import com.sevenGroup.pawnshop.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  商品操作的前端控制器
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "商品相关接口")
public class GoodsController {
    @Resource
    private GoodsService goodsService;
//  新增商品
    @PostMapping("saveShop")
    @ApiOperation("新增商品")
    @PreAuthorize("hasAnyAuthority('boss','shopManage','worker')")
    public Result saveShop(@RequestBody @ApiParam("商品对象") GoodsForm goodsForm){
        System.out.println("=========="+goodsForm);
        Integer integer=goodsService.saveShop(goodsForm);
        return Result.success(integer);
    }

//  通过分页查询所有商品
    @PostMapping("selectAllShops")
    @ApiOperation("查询商品")
    @PreAuthorize("hasAnyAuthority('boss','shopManage','worker')")
    public Result selectAllShops(@RequestBody @ApiParam("分页信息") Page page){
        IPage<GoodsInfo> goodsInfos=goodsService.selectAllShops(page);
        return Result.success(goodsInfos);
    }

//  根据条件查询商品（门店ID，仓库ID，商品大类ID，小类ID，商品ID等）
    @PostMapping("selectShopsByConditionals")
    @ApiOperation("通过条件查询商品")
    @PreAuthorize("hasAnyAuthority('boss','shopManage','worker')")
    public Result selectShopsByConditionals(@RequestBody @ApiParam("查询条件对象") ShopSelectConditionalForm selectConditionalForm){
        IPage<GoodsInfo> goodsInfos=goodsService.selectShopsByConditionals(selectConditionalForm);
        return Result.success(goodsInfos);
    }

    @ApiOperation("根据条件模糊查询")
    @PostMapping("selectGoodsByCondition")
    @PreAuthorize("hasAnyAuthority('boss','shopManage','worker')")
    public Result selectGoodsByCondition(@RequestBody ShopSelectConditionalForm shopSelectConditionalForm){
        System.out.println(shopSelectConditionalForm);
        IPage<GoodsInfo> goodsInfoIPage = goodsService.selectGoodsByCondition(shopSelectConditionalForm);
        return Result.success(goodsInfoIPage);
    }

    @ApiOperation("更新商品")
    @PostMapping("updateGoods")
    @PreAuthorize("hasAnyAuthority('boss','shopManage','worker')")
    public Result updateGoods(@RequestBody GoodsForm goodsForm){
        Integer num=goodsService.updateGoods(goodsForm);
        return Result.success(num);
    }

    @ApiOperation("根据集合删除商品")
    @PostMapping("delGoodsByList")
    public Result delGoodsByList(@RequestBody List<Integer> list){
        goodsService.removeByIds(list);
        return Result.success(null);
    }
}

